home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Utilities / vim-5.1 / src / mark.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-06  |  23.4 KB  |  980 lines

  1. /* vi:set ts=8 sts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved    by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  */
  8.  
  9. /*
  10.  * mark.c: functions for setting marks and jumping to them
  11.  */
  12.  
  13. #include "vim.h"
  14.  
  15. /*
  16.  * This file contains routines to maintain and manipulate marks.
  17.  */
  18.  
  19. /*
  20.  * If a named file mark's lnum is non-zero, it is valid.
  21.  * If a named file mark's fnum is non-zero, it is for an existing buffer,
  22.  * otherwise it is from .viminfo and namedfm_names[n] is the file name.
  23.  * There are marks 'A - 'Z (set by user) and '0 to '9 (set when writing
  24.  * viminfo).
  25.  */
  26. #define EXTRA_MARKS 10                    /* marks 0-9 */
  27. static struct filemark namedfm[NMARKS + EXTRA_MARKS];    /* marks with file nr */
  28. static char_u *namedfm_names[NMARKS + EXTRA_MARKS];    /* name for namedfm[] */
  29.  
  30. static void show_one_mark __ARGS((int, char_u *, FPOS *, char_u *));
  31. static void cleanup_jumplist __ARGS((void));
  32.  
  33. /*
  34.  * setmark(c) - set named mark 'c' at current cursor position
  35.  *
  36.  * Returns OK on success, FAIL if no room for mark or bad name given.
  37.  */
  38.     int
  39. setmark(c)
  40.     int        c;
  41. {
  42.     int        i;
  43.  
  44.     if (c == '\'' || c == '`')
  45.     {
  46.     setpcmark();
  47.     /* keep it even when the cursor doesn't move */
  48.     curwin->w_prev_pcmark = curwin->w_pcmark;
  49.     return OK;
  50.     }
  51.  
  52.     if (c > 'z')        /* some islower() and isupper() cannot handle
  53.                 characters above 127 */
  54.     return FAIL;
  55.     if (islower(c))
  56.     {
  57.     i = c - 'a';
  58.     curbuf->b_namedm[i] = curwin->w_cursor;
  59.     return OK;
  60.     }
  61.     if (isupper(c))
  62.     {
  63.     i = c - 'A';
  64.     namedfm[i].mark = curwin->w_cursor;
  65.     namedfm[i].fnum = curbuf->b_fnum;
  66.     return OK;
  67.     }
  68.     return FAIL;
  69. }
  70.  
  71. /*
  72.  * setpcmark() - set the previous context mark to the current position
  73.  *         and add it to the jump list
  74.  */
  75.     void
  76. setpcmark()
  77. {
  78.     int i;
  79. #ifdef ROTATE
  80.     struct filemark tempmark;
  81. #endif
  82.  
  83.     /* for :global the mark is set only once */
  84.     if (global_busy)
  85.     return;
  86.  
  87.     curwin->w_prev_pcmark = curwin->w_pcmark;
  88.     curwin->w_pcmark = curwin->w_cursor;
  89.  
  90. #ifdef ROTATE
  91.     /*
  92.      * If last used entry is not at the top, put it at the top by rotating
  93.      * the stack until it is (the newer entries will be at the bottom).
  94.      * Keep one entry (the last used one) at the top.
  95.      */
  96.     if (curwin->w_jumplistidx < curwin->w_jumplistlen)
  97.     ++curwin->w_jumplistidx;
  98.     while (curwin->w_jumplistidx < curwin->w_jumplistlen)
  99.     {
  100.     tempmark = curwin->w_jumplist[curwin->w_jumplistlen - 1];
  101.     for (i = curwin->w_jumplistlen - 1; i > 0; --i)
  102.         curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
  103.     curwin->w_jumplist[0] = tempmark;
  104.     ++curwin->w_jumplistidx;
  105.     }
  106. #endif
  107.  
  108.     /* If jumplist is full: remove oldest entry */
  109.     if (++curwin->w_jumplistlen > JUMPLISTSIZE)
  110.     {
  111.     curwin->w_jumplistlen = JUMPLISTSIZE;
  112.     for (i = 1; i < JUMPLISTSIZE; ++i)
  113.         curwin->w_jumplist[i - 1] = curwin->w_jumplist[i];
  114.     }
  115.     curwin->w_jumplistidx = curwin->w_jumplistlen - 1;
  116.  
  117. #ifdef ARCHIE
  118.     /* Workaround for a bug in gcc 2.4.5 R2 on the Archimedes
  119.      * Should be fixed in 2.5.x.
  120.      */
  121.     curwin->w_jumplist[curwin->w_jumplistidx].mark.ptr = curwin->w_pcmark.ptr;
  122.     curwin->w_jumplist[curwin->w_jumplistidx].mark.col = curwin->w_pcmark.col;
  123. #else
  124.     curwin->w_jumplist[curwin->w_jumplistidx].mark = curwin->w_pcmark;
  125. #endif
  126.     curwin->w_jumplist[curwin->w_jumplistidx].fnum = curbuf->b_fnum;
  127.     ++curwin->w_jumplistidx;
  128. }
  129.  
  130. /*
  131.  * checkpcmark() - To change context, call setpcmark(), then move the current
  132.  *           position to where ever, then call checkpcmark().  This
  133.  *           ensures that the previous context will only be changed if
  134.  *           the cursor moved to a different line. -- webb.
  135.  *           If pcmark was deleted (with "dG") the previous mark is
  136.  *           restored.
  137.  */
  138.     void
  139. checkpcmark()
  140. {
  141.     if (curwin->w_prev_pcmark.lnum != 0
  142.         && (equal(curwin->w_pcmark, curwin->w_cursor)
  143.         || curwin->w_pcmark.lnum == 0))
  144.     {
  145.     curwin->w_pcmark = curwin->w_prev_pcmark;
  146.     curwin->w_prev_pcmark.lnum = 0;        /* Show it has been checked */
  147.     }
  148. }
  149.  
  150. /*
  151.  * move "count" positions in the jump list (count may be negative)
  152.  */
  153.     FPOS *
  154. movemark(count)
  155.     int count;
  156. {
  157.     FPOS    *pos;
  158.  
  159.     cleanup_jumplist();
  160.  
  161.     if (curwin->w_jumplistlen == 0)        /* nothing to jump to */
  162.     return (FPOS *)NULL;
  163.  
  164.     if (curwin->w_jumplistidx + count < 0 ||
  165.             curwin->w_jumplistidx + count >= curwin->w_jumplistlen)
  166.     return (FPOS *)NULL;
  167.  
  168.     /*
  169.      * if first CTRL-O or CTRL-I command after a jump, add cursor position to
  170.      * list.  Careful: If there are duplicates (CTRL-O immidiately after
  171.      * starting Vim on a file), another entry may have been removed.
  172.      */
  173.     if (curwin->w_jumplistidx == curwin->w_jumplistlen)
  174.     {
  175.     setpcmark();
  176.     --curwin->w_jumplistidx;    /* skip the new entry */
  177.     if (curwin->w_jumplistidx + count < 0)
  178.         return (FPOS *)NULL;
  179.     }
  180.  
  181.     curwin->w_jumplistidx += count;
  182.                         /* jump to other file */
  183.     if (curwin->w_jumplist[curwin->w_jumplistidx].fnum != curbuf->b_fnum)
  184.     {
  185.     if (buflist_getfile(curwin->w_jumplist[curwin->w_jumplistidx].fnum,
  186.               curwin->w_jumplist[curwin->w_jumplistidx].mark.lnum,
  187.                                 0, FALSE) == FAIL)
  188.         return (FPOS *)NULL;
  189.     curwin->w_cursor.col =
  190.                curwin->w_jumplist[curwin->w_jumplistidx].mark.col;
  191.     pos = (FPOS *)-1;
  192.     }
  193.     else
  194.     pos = &(curwin->w_jumplist[curwin->w_jumplistidx].mark);
  195.     return pos;
  196. }
  197.  
  198. /*
  199.  * getmark(c) - find mark for char 'c'
  200.  *
  201.  * Return pointer to FPOS if found (caller needs to check lnum!)
  202.  *      NULL if there is no mark called 'c'.
  203.  *      -1 if mark is in other file (only if changefile is TRUE)
  204.  */
  205.     FPOS *
  206. getmark(c, changefile)
  207.     int        c;
  208.     int        changefile;        /* allowed to edit another file */
  209. {
  210.     FPOS        *posp;
  211.     FPOS        *startp, *endp;
  212.     static  FPOS    pos_copy;
  213.     char_u        *p;
  214.  
  215.     posp = NULL;
  216.     if (c > '~')            /* check for islower()/isupper() */
  217.     ;
  218.     else if (c == '\'' || c == '`')    /* previous context mark */
  219.     {
  220.     pos_copy = curwin->w_pcmark;    /* need to make a copy because */
  221.     posp = &pos_copy;        /*   w_pcmark may be changed soon */
  222.     }
  223.     else if (c == '"')            /* to pos when leaving buffer */
  224.     posp = &(curbuf->b_last_cursor);
  225.     else if (c == '[')            /* to start of previous operator */
  226.     posp = &(curbuf->b_op_start);
  227.     else if (c == ']')            /* to end of previous operator */
  228.     posp = &(curbuf->b_op_end);
  229.     else if (c == '<' || c == '>')    /* start/end of visual area */
  230.     {
  231.     startp = &curbuf->b_visual_start;
  232.     endp = &curbuf->b_visual_end;
  233.     if ((c == '<') == lt(*startp, *endp))
  234.         posp = startp;
  235.     else
  236.         posp = endp;
  237.     /*
  238.      * For Visual line mode, set mark at begin or end of line
  239.      */
  240.     if (curbuf->b_visual_mode == 'V')
  241.     {
  242.         pos_copy = *posp;
  243.         posp = &pos_copy;
  244.         if (c == '<')
  245.         pos_copy.col = 0;
  246.         else
  247.         pos_copy.col = MAXCOL;
  248.     }
  249.     }
  250.     else if (islower(c))        /* normal named mark */
  251.     posp = &(curbuf->b_namedm[c - 'a']);
  252.     else if (isupper(c) || vim_isdigit(c))    /* named file mark */
  253.     {
  254.     if (vim_isdigit(c))
  255.         c = c - '0' + NMARKS;
  256.     else
  257.         c -= 'A';
  258.     posp = &(namedfm[c].mark);
  259.  
  260.     if (namedfm[c].fnum == 0 && namedfm_names[c] != NULL)
  261.     {
  262.         /*
  263.          * First expand "~/" in the file name to the home directory.
  264.          * Try to shorten the file name.
  265.          */
  266.         expand_env(namedfm_names[c], NameBuff, MAXPATHL);
  267.         mch_dirname(IObuff, IOSIZE);
  268.         p = shorten_fname(NameBuff, IObuff);
  269.  
  270.         /* buflist_new will call fmarks_check_names() */
  271.         (void)buflist_new(NameBuff, p, (linenr_t)1, FALSE);
  272.     }
  273.  
  274.     if (namedfm[c].fnum != curbuf->b_fnum)        /* mark is in other file */
  275.     {
  276.         if (namedfm[c].mark.lnum != 0 && changefile && namedfm[c].fnum)
  277.         {
  278.         if (buflist_getfile(namedfm[c].fnum,
  279.                  namedfm[c].mark.lnum, GETF_SETMARK, FALSE) == OK)
  280.         {
  281.             curwin->w_cursor.col = namedfm[c].mark.col;
  282.             return (FPOS *)-1;
  283.         }
  284.         }
  285.         posp = &pos_copy;        /* mark exists, but is not valid in
  286.                         current buffer */
  287.         pos_copy.lnum = 0;
  288.     }
  289.     }
  290.     return posp;
  291. }
  292.  
  293. /*
  294.  * Check all file marks for a name that matches the file name in buf.
  295.  * May replace the name with an fnum.
  296.  */
  297.     void
  298. fmarks_check_names(buf)
  299.     BUF        *buf;
  300. {
  301.     char_u    *name;
  302.     int        i;
  303.  
  304.     if (buf->b_ffname == NULL)
  305.     return;
  306.  
  307.     name = home_replace_save(buf, buf->b_ffname);
  308.     if (name == NULL)
  309.     return;
  310.  
  311.     for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
  312.     {
  313.     if (namedfm[i].fnum == 0 && namedfm_names[i] != NULL &&
  314.                     fnamecmp(name, namedfm_names[i]) == 0)
  315.     {
  316.         namedfm[i].fnum = buf->b_fnum;
  317.         vim_free(namedfm_names[i]);
  318.         namedfm_names[i] = NULL;
  319.     }
  320.     }
  321.     vim_free(name);
  322. }
  323.  
  324. /*
  325.  * Check a if a position from a mark is valid.
  326.  * Give and error message and return FAIL if not.
  327.  */
  328.     int
  329. check_mark(pos)
  330.     FPOS    *pos;
  331. {
  332.     if (pos == NULL)
  333.     {
  334.     emsg(e_umark);
  335.     return FAIL;
  336.     }
  337.     if (pos->lnum == 0)
  338.     {
  339.     emsg(e_marknotset);
  340.     return FAIL;
  341.     }
  342.     if (pos->lnum > curbuf->b_ml.ml_line_count)
  343.     {
  344.     emsg(e_markinval);
  345.     return FAIL;
  346.     }
  347.     return OK;
  348. }
  349.  
  350. /*
  351.  * clrallmarks() - clear all marks in the buffer 'buf'
  352.  *
  353.  * Used mainly when trashing the entire buffer during ":e" type commands
  354.  */
  355.     void
  356. clrallmarks(buf)
  357.     BUF        *buf;
  358. {
  359.     static int        i = -1;
  360.  
  361.     if (i == -1)    /* first call ever: initialize */
  362.     for (i = 0; i < NMARKS + 1; i++)
  363.     {
  364.         namedfm[i].mark.lnum = 0;
  365.         namedfm_names[i] = NULL;
  366.     }
  367.  
  368.     for (i = 0; i < NMARKS; i++)
  369.     buf->b_namedm[i].lnum = 0;
  370.     buf->b_op_start.lnum = 0;        /* start/end op mark cleared */
  371.     buf->b_op_end.lnum = 0;
  372.     buf->b_last_cursor.lnum = 1;    /* '" mark cleared */
  373.     buf->b_last_cursor.col = 0;
  374. }
  375.  
  376. /*
  377.  * Get name of file from a filemark.
  378.  * Returns an allocated string.
  379.  */
  380.     char_u *
  381. fm_getname(fmark)
  382.     struct filemark *fmark;
  383. {
  384.     if (fmark->fnum != curbuf->b_fnum)            /* not current file */
  385.     return buflist_nr2name(fmark->fnum, FALSE, TRUE);
  386.     return vim_strsave((char_u *)"-current-");
  387. }
  388.  
  389. /*
  390.  * print the marks
  391.  */
  392.     void
  393. do_marks(arg)
  394.     char_u    *arg;
  395. {
  396.     int        i;
  397.     char_u    *name;
  398.  
  399.     if (arg != NULL && *arg == NUL)
  400.     arg = NULL;
  401.  
  402.     show_one_mark('\'', arg, &curwin->w_pcmark, NULL);
  403.     for (i = 0; i < NMARKS; ++i)
  404.     show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL);
  405.     for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
  406.     {
  407.     name = namedfm[i].fnum ? fm_getname(&namedfm[i]) : namedfm_names[i];
  408.     if (name != NULL)
  409.     {
  410.         show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A',
  411.                          arg, &namedfm[i].mark, name);
  412.         if (namedfm[i].fnum)
  413.         vim_free(name);
  414.     }
  415.     }
  416.     show_one_mark('"', arg, &curbuf->b_last_cursor, NULL);
  417.     show_one_mark('[', arg, &curbuf->b_op_start, NULL);
  418.     show_one_mark(']', arg, &curbuf->b_op_end, NULL);
  419.     show_one_mark('<', arg, &curbuf->b_visual_start, NULL);
  420.     show_one_mark('>', arg, &curbuf->b_visual_end, NULL);
  421.     show_one_mark(-1, arg, NULL, NULL);
  422. }
  423.  
  424.     static void
  425. show_one_mark(c, arg, p, name)
  426.     int        c;
  427.     char_u  *arg;
  428.     FPOS    *p;
  429.     char_u  *name;
  430. {
  431.     static int        did_title = FALSE;
  432.  
  433.     if (c == -1)                /* finish up */
  434.     {
  435.     if (did_title)
  436.         did_title = FALSE;
  437.     else
  438.     {
  439.         if (arg == NULL)
  440.         MSG("No marks set");
  441.         else
  442.         EMSG2("No marks matching \"%s\"", arg);
  443.     }
  444.     }
  445.     /* don't output anything if 'q' typed at --more-- prompt */
  446.     else if (!got_int && (arg == NULL || vim_strchr(arg, c) != NULL) &&
  447.                                  p->lnum != 0)
  448.     {
  449.     if (!did_title)
  450.     {
  451.         /* Highlight title */
  452.         MSG_PUTS_TITLE("\nmark line  col file");
  453.         did_title = TRUE;
  454.     }
  455.     msg_putchar('\n');
  456.     if (!got_int)
  457.     {
  458.         sprintf((char *)IObuff, " %c %5ld  %3d  ", c, p->lnum, p->col);
  459.         if (name != NULL)
  460.         STRCAT(IObuff, name);
  461.         msg_outtrans(IObuff);
  462.     }
  463.     out_flush();            /* show one line at a time */
  464.     }
  465. }
  466.  
  467. /*
  468.  * print the jumplist
  469.  */
  470.     void
  471. do_jumps()
  472. {
  473.     int        i;
  474.     char_u    *name;
  475.  
  476.     cleanup_jumplist();
  477.     /* Highlight title */
  478.     MSG_PUTS_TITLE("\n jump line  file");
  479.     for (i = 0; i < curwin->w_jumplistlen; ++i)
  480.     {
  481.     if (curwin->w_jumplist[i].mark.lnum != 0)
  482.     {
  483.         name = fm_getname(&curwin->w_jumplist[i]);
  484.         if (name == NULL)        /* file name not available */
  485.         continue;
  486.  
  487.         msg_putchar('\n');
  488.         sprintf((char *)IObuff, "%c %2d %5ld  %s",
  489.         i == curwin->w_jumplistidx ? '>' : ' ',
  490.         i > curwin->w_jumplistidx ? i - curwin->w_jumplistidx
  491.                       : curwin->w_jumplistidx - i,
  492.         curwin->w_jumplist[i].mark.lnum,
  493.         name);
  494.         msg_outtrans(IObuff);
  495.         vim_free(name);
  496.     }
  497.     out_flush();
  498.     }
  499.     if (curwin->w_jumplistidx == curwin->w_jumplistlen)
  500.     MSG_PUTS("\n>");
  501. }
  502.  
  503. /*
  504.  * adjust marks between line1 and line2 (inclusive) to move 'amount' lines
  505.  * If 'amount' is MAXLNUM the mark is made invalid.
  506.  * If 'amount_after' is non-zero adjust marks after line 2.
  507.  */
  508.  
  509. #define one_adjust(add) \
  510.     { \
  511.     lp = add; \
  512.     if (*lp >= line1 && *lp <= line2) \
  513.     { \
  514.         if (amount == MAXLNUM) \
  515.         *lp = 0; \
  516.         else \
  517.         *lp += amount; \
  518.     } \
  519.     else if (amount_after && *lp > line2) \
  520.         *lp += amount_after; \
  521.     }
  522.  
  523. /* don't delete the line, just put at first deleted line */
  524. #define one_adjust_nodel(add) \
  525.     { \
  526.     lp = add; \
  527.     if (*lp >= line1 && *lp <= line2) \
  528.     { \
  529.         if (amount == MAXLNUM) \
  530.         *lp = line1; \
  531.         else \
  532.         *lp += amount; \
  533.     } \
  534.     else if (amount_after && *lp > line2) \
  535.         *lp += amount_after; \
  536.     }
  537.  
  538.     void
  539. mark_adjust(line1, line2, amount, amount_after)
  540.     linenr_t    line1;
  541.     linenr_t    line2;
  542.     long    amount;
  543.     long    amount_after;
  544. {
  545.     int        i;
  546.     int        fnum = curbuf->b_fnum;
  547.     linenr_t    *lp;
  548.     WIN        *win;
  549.  
  550.     if (line2 < line1 && amount_after == 0L)        /* nothing to do */
  551.     return;
  552.  
  553. /* named marks, lower case and upper case */
  554.     for (i = 0; i < NMARKS; i++)
  555.     {
  556.     one_adjust(&(curbuf->b_namedm[i].lnum));
  557.     if (namedfm[i].fnum == fnum)
  558.         one_adjust(&(namedfm[i].mark.lnum));
  559.     }
  560.     for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
  561.     {
  562.     if (namedfm[i].fnum == fnum)
  563.         one_adjust(&(namedfm[i].mark.lnum));
  564.     }
  565.  
  566. /* previous context mark */
  567.     one_adjust(&(curwin->w_pcmark.lnum));
  568.  
  569. /* previous pcmark */
  570.     one_adjust(&(curwin->w_prev_pcmark.lnum));
  571.  
  572. /* Visual area */
  573.     one_adjust_nodel(&(curbuf->b_visual_start.lnum));
  574.     one_adjust_nodel(&(curbuf->b_visual_end.lnum));
  575.  
  576. /* marks in the tag stack */
  577.     for (win = firstwin; win != NULL; win = win->w_next)
  578.     if (win->w_buffer == curbuf)
  579.         for (i = 0; i < win->w_tagstacklen; i++)
  580.         if (win->w_tagstack[i].fmark.fnum == fnum)
  581.             one_adjust_nodel(&(win->w_tagstack[i].fmark.mark.lnum));
  582.  
  583. #ifdef QUICKFIX
  584. /* quickfix marks */
  585.     qf_mark_adjust(line1, line2, amount, amount_after);
  586. #endif
  587.  
  588. /* jumplist marks */
  589.     for (win = firstwin; win != NULL; win = win->w_next)
  590.     {
  591.     /*
  592.      * When deleting lines, this may create duplicate marks in the
  593.      * jumplist. They will be removed later.
  594.      */
  595.     for (i = 0; i < win->w_jumplistlen; ++i)
  596.         if (win->w_jumplist[i].fnum == fnum)
  597.         one_adjust_nodel(&(win->w_jumplist[i].mark.lnum));
  598.     /*
  599.      * also adjust the line at the top of the window and the cursor
  600.      * position for windows with the same buffer.
  601.      */
  602.     if (win != curwin && win->w_buffer == curbuf)
  603.     {
  604.         if (win->w_topline >= line1 && win->w_topline <= line2)
  605.         {
  606.         if (amount == MAXLNUM)        /* topline is deleted */
  607.         {
  608.             if (line1 <= 1)
  609.             win->w_topline = 1;
  610.             else
  611.             win->w_topline = line1 - 1;
  612.         }
  613.         else            /* keep topline on the same line */
  614.             win->w_topline += amount;
  615.         }
  616.         else if (amount_after && win->w_topline > line2)
  617.         win->w_topline += amount_after;
  618.         if (win->w_cursor.lnum >= line1 && win->w_cursor.lnum <= line2)
  619.         {
  620.         if (amount == MAXLNUM)        /* line with cursor is deleted */
  621.         {
  622.             if (line1 <= 1)
  623.             win->w_cursor.lnum = 1;
  624.             else
  625.             win->w_cursor.lnum = line1 - 1;
  626.             win->w_cursor.col = 0;
  627.         }
  628.         else            /* keep cursor on the same line */
  629.             win->w_cursor.lnum += amount;
  630.         }
  631.         else if (amount_after && win->w_cursor.lnum > line2)
  632.         win->w_cursor.lnum += amount_after;
  633.     }
  634.     }
  635. }
  636.  
  637. /*
  638.  * When deleting lines, this may create duplicate marks in the
  639.  * jumplist. They will be removed here for the current window.
  640.  */
  641.     static void
  642. cleanup_jumplist()
  643. {
  644.     int        i;
  645.     int        from, to;
  646.  
  647.     to = 0;
  648.     for (from = 0; from < curwin->w_jumplistlen; ++from)
  649.     {
  650.     if (curwin->w_jumplistidx == from)
  651.         curwin->w_jumplistidx = to;
  652.     for (i = from + 1; i < curwin->w_jumplistlen; ++i)
  653.         if (curwin->w_jumplist[i].fnum == curwin->w_jumplist[from].fnum &&
  654.         curwin->w_jumplist[i].mark.lnum ==
  655.                        curwin->w_jumplist[from].mark.lnum)
  656.         break;
  657.     if (i >= curwin->w_jumplistlen)        /* no duplicate */
  658.         curwin->w_jumplist[to++] = curwin->w_jumplist[from];
  659.     }
  660.     if (curwin->w_jumplistidx == curwin->w_jumplistlen)
  661.     curwin->w_jumplistidx = to;
  662.     curwin->w_jumplistlen = to;
  663. }
  664.  
  665.     void
  666. set_last_cursor(win)
  667.     WIN        *win;
  668. {
  669.     win->w_buffer->b_last_cursor = win->w_cursor;
  670. }
  671.  
  672. #ifdef VIMINFO
  673.     int
  674. read_viminfo_filemark(line, fp, force)
  675.     char_u  *line;
  676.     FILE    *fp;
  677.     int        force;
  678. {
  679.     int        idx;
  680.     char_u  *str;
  681.  
  682.     /* We only get here (hopefully) if line[0] == '\'' */
  683.     str = line + 1;
  684.     if (*str > 127 || (!isdigit(*str) && !isupper(*str)))
  685.     {
  686.     if (viminfo_error("Illegal file mark name", line))
  687.         return TRUE;    /* Too many errors, pretend end-of-file */
  688.     }
  689.     else
  690.     {
  691.     if (isdigit(*str))
  692.         idx = *str - '0' + NMARKS;
  693.     else
  694.         idx = *str - 'A';
  695.     if (namedfm[idx].mark.lnum == 0 || force)
  696.     {
  697.         str = skipwhite(str + 1);
  698.         namedfm[idx].mark.lnum = getdigits(&str);
  699.         str = skipwhite(str);
  700.         namedfm[idx].mark.col = getdigits(&str);
  701.         str = skipwhite(str);
  702.         viminfo_readstring(line);
  703.         namedfm_names[idx] = vim_strsave(str);
  704.     }
  705.     }
  706.     return vim_fgets(line, LSIZE, fp);
  707. }
  708.  
  709.     void
  710. write_viminfo_filemarks(fp)
  711.     FILE    *fp;
  712. {
  713.     int        i;
  714.     char_u  *name;
  715.  
  716.     if (get_viminfo_parameter('\'') == 0)
  717.     return;
  718.  
  719.     fprintf(fp, "\n# File marks:\n");
  720.  
  721.     /*
  722.      * Find a mark that is the same file and position as the cursor.
  723.      * That one, or else the last one is deleted.
  724.      * Move '0 to '1, '1 to '2, etc. until the matching one or '9
  725.      * Set '0 mark to current cursor position.
  726.      */
  727.     if (curbuf->b_ffname != NULL && !removable(curbuf->b_ffname))
  728.     {
  729.     name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
  730.     for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
  731.         if (namedfm[i].mark.lnum == curwin->w_cursor.lnum
  732.             && (namedfm_names[i] == NULL
  733.                 ? namedfm[i].fnum == curbuf->b_fnum
  734.                 : (name != NULL
  735.                     && STRCMP(name, namedfm_names[i]) == 0)))
  736.         break;
  737.     vim_free(name);
  738.  
  739.     vim_free(namedfm_names[i]);
  740.     for ( ; i > NMARKS; --i)
  741.     {
  742.         namedfm[i] = namedfm[i - 1];
  743.         namedfm_names[i] = namedfm_names[i - 1];
  744.     }
  745.     namedfm[NMARKS].mark = curwin->w_cursor;
  746.     namedfm[NMARKS].fnum = curbuf->b_fnum;
  747.     namedfm_names[NMARKS] = NULL;
  748.     }
  749.  
  750.     for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
  751.     {
  752.     if (namedfm[i].mark.lnum == 0)        /* not set */
  753.         continue;
  754.  
  755.     if (namedfm[i].fnum)            /* there is a buffer */
  756.         name = buflist_nr2name(namedfm[i].fnum, TRUE, FALSE);
  757.     else
  758.         name = namedfm_names[i];        /* use name from .viminfo */
  759.     if (name == NULL)
  760.         continue;
  761.  
  762.     fprintf(fp, "'%c  %ld  %ld  %s\n",
  763.             i < NMARKS ? i + 'A' : i - NMARKS + '0',
  764.             (long)namedfm[i].mark.lnum,
  765.             (long)namedfm[i].mark.col,
  766.             name);
  767.     if (namedfm[i].fnum)
  768.         vim_free(name);
  769.     }
  770. }
  771.  
  772. /*
  773.  * Return TRUE if "name" is on removable media (depending on 'viminfo').
  774.  */
  775.     int
  776. removable(name)
  777.     char_u  *name;
  778. {
  779.     char_u  *p;
  780.     char_u  part[51];
  781.     int        retval = FALSE;
  782.  
  783.     name = home_replace_save(NULL, name);
  784.     if (name != NULL)
  785.     {
  786.     for (p = p_viminfo; *p; )
  787.     {
  788.         copy_option_part(&p, part, 51, ", ");
  789.         if (part[0] == 'r'
  790.                && STRNICMP(part + 1, name, STRLEN(part + 1)) == 0)
  791.         {
  792.         retval = TRUE;
  793.         break;
  794.         }
  795.     }
  796.     vim_free(name);
  797.     }
  798.     return retval;
  799. }
  800.  
  801. /*
  802.  * Write all the named marks for all buffers.
  803.  * Return the number of buffers for which marks have been written.
  804.  */
  805.     int
  806. write_viminfo_marks(fp_out)
  807.     FILE    *fp_out;
  808. {
  809.     int        count;
  810.     BUF        *buf;
  811.     WIN        *win;
  812.     int        is_mark_set;
  813.     int        i;
  814.  
  815.     /*
  816.      * Set b_last_cursor for the all buffers that have a window.
  817.      */
  818.     for (win = firstwin; win != NULL; win = win->w_next)
  819.     set_last_cursor(win);
  820.  
  821.     fprintf(fp_out, "\n# History of marks within files (newest to oldest):\n");
  822.     count = 0;
  823.     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  824.     {
  825.     /*
  826.      * Only write something if buffer has been loaded and at least one
  827.      * mark is set.
  828.      */
  829.     if (buf->b_marks_read)
  830.     {
  831.         if (buf->b_last_cursor.lnum != 0)
  832.         is_mark_set = TRUE;
  833.         else
  834.         {
  835.         is_mark_set = FALSE;
  836.         for (i = 0; i < NMARKS; i++)
  837.             if (buf->b_namedm[i].lnum != 0)
  838.             {
  839.             is_mark_set = TRUE;
  840.             break;
  841.             }
  842.         }
  843.         if (is_mark_set && buf->b_ffname != NULL &&
  844.          buf->b_ffname[0] != NUL && !removable(buf->b_ffname))
  845.         {
  846.         home_replace(NULL, buf->b_ffname, IObuff, IOSIZE);
  847.         fprintf(fp_out, "\n> %s\n", (char *)IObuff);
  848.         if (buf->b_last_cursor.lnum != 0)
  849.             fprintf(fp_out, "\t\"\t%ld\t%d\n",
  850.                 buf->b_last_cursor.lnum, buf->b_last_cursor.col);
  851.         for (i = 0; i < NMARKS; i++)
  852.             if (buf->b_namedm[i].lnum != 0)
  853.             fprintf(fp_out, "\t%c\t%ld\t%d\n", 'a' + i,
  854.                 buf->b_namedm[i].lnum, buf->b_namedm[i].col);
  855.         count++;
  856.         }
  857.     }
  858.     }
  859.  
  860.     return count;
  861. }
  862.  
  863. /*
  864.  * Handle marks in the viminfo file:
  865.  * fp_out == NULL   read marks for current buffer only
  866.  * fp_out != NULL   copy marks for buffers not in buffer list
  867.  */
  868.     void
  869. copy_viminfo_marks(line, fp_in, fp_out, count, eof)
  870.     char_u    *line;
  871.     FILE    *fp_in;
  872.     FILE    *fp_out;
  873.     int        count;
  874.     int        eof;
  875. {
  876.     BUF        *buf;
  877.     int        num_marked_files;
  878.     char_u    save_char;
  879.     int        load_marks;
  880.     int        copy_marks_out;
  881.     char_u    *str;
  882.     int        i;
  883.     char_u    *p;
  884.     char_u    *name_buf;
  885.     long    lnum;
  886.     int        col;
  887.  
  888.     if ((name_buf = alloc(LSIZE)) == NULL)
  889.     return;
  890.     num_marked_files = get_viminfo_parameter('\'');
  891.     while (!eof && (count < num_marked_files || fp_out == NULL))
  892.     {
  893.     if (line[0] != '>')
  894.     {
  895.         if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
  896.         {
  897.         if (viminfo_error("Missing '>'", line))
  898.             break;    /* too many errors, return now */
  899.         }
  900.         eof = vim_fgets(line, LSIZE, fp_in);
  901.         continue;        /* Skip this dud line */
  902.     }
  903.  
  904.     /*
  905.      * Find file name, set str to start.
  906.      * Ignore leading and trailing white space.
  907.      */
  908.     str = skipwhite(line + 1);
  909.     p = str + STRLEN(str);
  910.     while (p != str && (*p == NUL || vim_isspace(*p)))
  911.         p--;
  912.     if (*p)
  913.         p++;
  914.     save_char = *p;
  915.     *p = NUL;
  916.  
  917.     /*
  918.      * If fp_out == NULL, load marks for current buffer.
  919.      * If fp_out != NULL, copy marks for buffers not in buflist.
  920.      */
  921.     load_marks = copy_marks_out = FALSE;
  922.     if (fp_out == NULL)
  923.     {
  924.         if (curbuf->b_ffname != NULL)
  925.         {
  926.         home_replace(NULL, curbuf->b_ffname, name_buf, LSIZE);
  927.         if (fnamecmp(str, name_buf) == 0)
  928.             load_marks = TRUE;
  929.         }
  930.     }
  931.     else /* fp_out != NULL */
  932.     {
  933.         /* This is slow if there are many buffers!! */
  934.         for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  935.         if (buf->b_ffname != NULL)
  936.         {
  937.             home_replace(NULL, buf->b_ffname, name_buf, LSIZE);
  938.             if (fnamecmp(str, name_buf) == 0)
  939.             break;
  940.         }
  941.  
  942.         /*
  943.          * copy marks if the buffer has not been loaded
  944.          */
  945.         if (buf == NULL || !buf->b_marks_read)
  946.         {
  947.         copy_marks_out = TRUE;
  948.         *p = save_char;
  949.         fputs("\n", fp_out);
  950.         fputs((char *)line, fp_out);
  951.         count++;
  952.         }
  953.     }
  954.     while (!(eof = vim_fgets(line, LSIZE, fp_in)) && line[0] == TAB)
  955.     {
  956.         if (load_marks)
  957.         {
  958.         if (line[1] != NUL)
  959.             sscanf((char *)line + 2, "%ld %d", &lnum, &col);
  960.         if (line[1] == '"')
  961.         {
  962.             curbuf->b_last_cursor.lnum = lnum;
  963.             curbuf->b_last_cursor.col = col;
  964.         }
  965.         else if ((i = line[1] - 'a') >= 0 && i < NMARKS)
  966.         {
  967.             curbuf->b_namedm[i].lnum = lnum;
  968.             curbuf->b_namedm[i].col = col;
  969.         }
  970.         }
  971.         else if (copy_marks_out)
  972.         fputs((char *)line, fp_out);
  973.     }
  974.     if (load_marks)
  975.         break;
  976.     }
  977.     vim_free(name_buf);
  978. }
  979. #endif /* VIMINFO */
  980.